package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        String username = employee.getUsername();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        Employee emp = employeeService.getOne(queryWrapper);
        //1.用户名不存在
        if(emp==null){
            return R.error("用户名不存在");
        }
        //2.密码错误
        String pw1 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());//密码需要md5加密
        String pw2 = emp.getPassword();
        if(!pw1.equals(pw2)){
            return R.error("密码错误");
        }
        //3.账户被禁用
        if(emp.getStatus()!=1){
            return R.error("账号被禁用");
        }
        //4.登录成功        并将员工ID存入session
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     */

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
    //    销毁session
        session.invalidate();
    //    返回结果
        return R.success("退出成功");
    }

    /**
     * 添加员工
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpSession session){
        //补全相关数据
        //补全密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //补全修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setCreateUser((Long) session.getAttribute("employee"));
        //employee.setUpdateUser((Long) session.getAttribute("employee"));

        //调用业务层相关数据
        employeeService.save(employee);

        return R.success(" ");
    }
    /**
     * 分页条件查询
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(String name,Integer page,Integer pageSize){

        //构造分页对象
        Page<Employee> pageInfo=new Page<>(page,pageSize);

    //    构造查询条件
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    /**
     * 更新员工信息
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
    //    设置更新时间和人
    //    employee.setUpdateTime(LocalDateTime.now());
    //    employee.setUpdateUser((Long) session.getAttribute("employee"));

        employeeService.updateById(employee);
        //由于ID的大小长度超过了前端的范围，所以需要配置类来将ID的long类型修改为String类型
        return R.success("更新成功");
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable("id")Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
}